package dao;

import bean.LoginInformation;
import bean.User;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.DruidUtils;

import javax.swing.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 2:42 下午 2021/5/27
 */
public class UserDaoImp implements UserDao {
    @Override
    public List<LoginInformation> findManager() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from manager ";
        List<LoginInformation> list = null;
        try {
            list = queryRunner.query(sql, new BeanListHandler<>(LoginInformation.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<User> getAllUser( int page) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from user limit ?,?";
        List<User> list = null;
        try {
            list = queryRunner.query(sql, new BeanListHandler<>(User.class),page,page+2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int addUser(User user) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "INSERT INTO user(sno,name,sex,birthday,mail,remark) VALUES(?,?,?,?,?,?)";
        Object[] param = {user.getSno(), user.getName(), user.getSex(), user.getBirthday(), user.getMail(), user.getRemark()};
        Connection connection = DruidUtils.getConnection();
        int i = 0;
        try {
            i = queryRunner.update(connection, sql, param);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DbUtils.closeQuietly(connection);
        return i;
    }

    @Override
    public int deleteUser(int sno) {
        int i = 0;
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "delete from user where sno=?";
        try {
            i = queryRunner.update(sql, sno);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<User> selectUserBySno(int sno) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from user where sno=?";
        try {
            List<User> list = queryRunner.query(sql, new BeanListHandler<>(User.class), sno);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateUser(User user) {
        int i = 0;
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "update user set name=?,sex=?,birthday=?,mail=?,remark=? where sno=?";
        Object[] param={user.getName(),user.getSex(),user.getBirthday(),user.getMail(),user.getRemark(),user.getSno()};
        try {
            i=queryRunner.update(sql,param);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
